#include "mf_csl_functions.h"

int ADC_init(CSL_SarHandleObj *SarHandle)
{
	CSL_SarChSetup param;
	int chanNo;
	
	if(CSL_init()) return 1;
	
	if(CSL_Open_Channel(SarHandle)) return 1;
	//printf("OPEN_CHANNEL OK\n");
	
	if(CSL_Channel_Init(SarHandle)) return 1;
	//printf("CHANNEL_INIT OK\n");
	
	param.OpMode = CSL_SAR_POLLING;
    param.MultiCh = CSL_SAR_NO_DISCHARGE;
    param.RefVoltage = CSL_SAR_REF_VIN;
    param.SysClkDiv = 0x64 ;
	
	if(CSL_Channel_Setup(SarHandle, &param)) return 1;
	//printf("CHANNEL_SETUP OK\n");
	
	if(CSL_Set_Channel_Cycling(SarHandle)) return 1;
	//printf("CHANNEL_CYCLING OK\n");
	
	if(CSL_Set_ADC_Measurement(SarHandle, &chanNo)) return 1;
	//printf("Channel Number selected %d\n",chanNo);
	
	if(CSL_Start_Conversion(SarHandle)) return 1;
	
	return 0;
}

int ADC_deInit(CSL_SarHandleObj *SarHandle)
{
	if(CSL_Stop_Conversion(SarHandle)) return 1;
	if(CSL_Close_Channel(SarHandle)) return 1;
	if(CSL_deInit()) return 1;
	
	return 0;

}

int ADC_read(CSL_SarHandleObj *SarHandle, Uint16 *readBuffer)
{
	CSL_Status status;
	
	while(CSL_SAR_DATA_AVAILABLE != SAR_getStatus(SarHandle, &status));
	
	status = SAR_readData(SarHandle, readBuffer);
    if(status != CSL_SOK)
    {
        printf("SAR_readData Failed!!\n");
        return(1);
    }
	
	//printf("SAR ADC read data %x\n", *readBuffer);
	
	return 0;
}

//////////////////////////////////////////////////
//
//	CSL Functions: Taken from csl_sar_PollExample.c
//
//////////////////////////////////////////////////
int CSL_init()
{
	CSL_Status status;
	
	/* Initialize the SAR module */
    status = SAR_init();
    if(status != CSL_SOK)
    {
        printf("SAR Init Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_deInit()
{
	CSL_Status status;
	
	status = SAR_deInit();
    if(status != CSL_SOK)
    {
        printf("SAR_deInit Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Open_Channel(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;
	
	status = SAR_chanOpen(SarHandle, CSL_SAR_CHAN_1);
    if(status != CSL_SOK)
    {
        printf("SAR_chanOpen Failed!!\n");
        return 1;
    }
	
	return 0;
}

int CSL_Close_Channel(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;
	
	status = SAR_chanClose(SarHandle);
    if(status != CSL_SOK)
    {
        printf("SAR_chanClose Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Channel_Init(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;
	
	status = SAR_chanInit(SarHandle);
    if(status != CSL_SOK)
    {
        printf("SAR_chanInit Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Channel_Setup(CSL_SarHandleObj *SarHandle, CSL_SarChSetup *param)
{
	CSL_Status status;
	
	status = SAR_chanSetup(SarHandle, param);
    if(status != CSL_SOK)
    {
        printf("SAR_chanConfig Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Set_Channel_Cycling(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;

	status = SAR_chanCycSet(SarHandle, CSL_SAR_CONTINUOUS_CONVERSION);
    if(status != CSL_SOK)
    {
        printf("SAR_chanCycSet Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Set_ADC_Measurement(CSL_SarHandleObj *SarHandle, int *chanNo)
{
	CSL_Status status;

	status = SAR_A2DMeasParamSet(SarHandle, CSL_KEYPAD_MEAS, chanNo);
    if(status != CSL_SOK)
    {
        printf("SAR_A2DMeasParamSet Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Start_Conversion(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;
	
	status = SAR_startConversion(SarHandle);
    if(status != CSL_SOK)
    {
        printf("SAR_startConversion Failed!!\n");
        return(1);
    }
	
	return 0;
}

int CSL_Stop_Conversion(CSL_SarHandleObj *SarHandle)
{
	CSL_Status status;
	
	status = SAR_stopConversion(SarHandle);
    if(status != CSL_SOK)
    {
        printf("SAR_stopConversion Failed!!\n");
        return(1);
    }
	
	return 0;
}
